package com.etime.shycourse.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shycourse.config.Tools;
import com.etime.shycourse.dao.OrganizationMapper;
import com.etime.shycourse.dto.CurrentUser;
import com.etime.shycourse.dto.OrgTeacherDtoS;
import com.etime.shycourse.pojo.Organization;
import com.etime.shycourse.services.interfaces.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author wb
 * @since 2021-12-14
 */
@Service
public class OrganizationServiceImpl implements OrganizationService {
    @Autowired
    private OrganizationMapper mapper;
    @Autowired
    private Tools tools;

    /**
     * 查询机构信息 改为token 22/1/21
     * 根据机构负责人用户id（orguserid）=登录用户id（userid）
     * 改为token 22/1/21
     *
     * @return Organization
     */
    @Override
    public Organization selectOrgByUserId(String token) {
        //改为token 22/1/21
        CurrentUser user = tools.getCurrentUserFromRedis(token);
        // 用户信息
        if (user == null) {
            return null;
        }
        // 用户信息 根据用户id进行查询
        if (user.getUser().getUserid() != null) {
            return mapper.selectOne(new QueryWrapper<Organization>().eq("orguserid", user.getUser().getUserid()));
        }
        return null;
    }

    /**
     * 查询机构信息
     * <p>
     * 根据机构 Id
     *
     * @return Organization
     */
    @Override
    public Organization selectById(Integer orgId) {
        return mapper.selectByIds(orgId);
    }

    /**
     * 根据机构id查询一条机构的详细信息
     *
     * @param orgid
     * @return
     */
    @Override
    public Organization selectOrganizationById(Integer orgid) {
        return mapper.selectById(orgid);
    }

    /**
     * 修改机构 信息
     * 机构负责人可以进行修改
     *
     * @param org 用户实体
     * @return 0失败；1成功
     */
    @Override
    public int updateOrganization(Organization org) {
        return mapper.updateById(org);
    }

    /**
     * 添加机构
     *
     * @param org 用户实体
     * @return 0失败；1成功
     */
    @Override
    public int insetOrganization(Organization org) {
        //审核状态
        org.setOrgauditstate(1);
        //
        if (org.getOrgtakepoint() == null) {
            org.setOrgtakepoint(new BigDecimal(-1));
        }
        return mapper.insert(org);
    }

    /**
     * 查询所有机构
     *
     * @return List<Organization>
     */
    @Override
    public List<Organization> selectOrganizationAllList() {
        return mapper.selectList(null);
    }


    /**
     * 机构管理 分页管理
     * 条件：
     * 1、机构名称
     *
     * @param organ
     * @return Page<OrgTeacherDtoS>
     */
    @Override
    public Page<OrgTeacherDtoS> selectOrganizationPage(Organization organ, Long pageNum, Long pageSize) {
        Page<OrgTeacherDtoS> page;
        if (pageNum != null & pageSize != null) {
            page = new Page<>(pageNum, pageSize);
        } else {
            page = new Page<>();
        }
        if (organ.getOrgname() != null) {
            organ.setOrgname("%" + organ.getOrgname() + "%");
        }
        return mapper.selectOrganizationPage(page, organ);
    }
}
